package com.common;

import java.io.Serializable;

/**
 * 公共返回对象
 * @author Clay
 */
public class Result implements Serializable {

    //返回给前端的编码
    private int code;
    //消息
    private String msg;
    //返回给前端数据
    private Object data;

    public static Result success(int code,String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static Result success() {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.code);
        result.setData(null);
        result.setMsg(ResultEnum.SUCCESS.msg);
        return result;
    }

    public static Result success(String msg, Object data) {
        Result result = new Result();
        result.setCode(200);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static Result success(Object data) {
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.code);
        result.setData(data);
        result.setMsg(ResultEnum.SUCCESS.msg);
        return result;
    }

    public static Result fail(int code,String msg, Object data) {
        Result result = new Result();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }

    public static Result fail(int code,String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setData(null);
        result.setMsg(msg);
        return result;
    }

    public static Result fail(String code,String msg) {
        Result result = new Result();
        result.setCode(ResultEnum.FAIL.getCode());
        result.setData(null);
        result.setMsg(msg);
        return result;
    }

    public static Result fail(String msg) {
        Result result = new Result();
        result.setCode(ResultEnum.ERROR.code);
        result.setData(null);
        result.setMsg(msg);
        return result;
    }

    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", msg='" + msg + '\'' +
                ", data=" + data +
                '}';
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
